package freespiritsocial.io.user.ui.helper

import androidx.core.os.bundleOf
import freespiritsocial.io.user.R
import freespiritsocial.io.user.base.BaseFragment
import freespiritsocial.io.user.data.model.BtnItems
import freespiritsocial.io.user.data.model.bean.PostPojo
import freespiritsocial.io.user.ext.str
import freespiritsocial.io.user.ui.fragment.message.SharePostListDialogFragment
import freespiritsocial.io.user.ui.fragment.post.PostDetailFrg
import freespiritsocial.io.user.viewmodel.delegates.IPostApi
import freespiritsocial.io.user.weight.BottomCancelDialog
import freespiritsocial.io.user.weight.BottomSheetListDialog
import freespiritsocial.io.user.weight.ReportPostDialog
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.ext.nav

class PostMoreDialogHelper(val context: BaseFragment<*,*>,val data: PostPojo,val model: IPostApi) {
    private val moreList
        get() = if (data.isMyPost().not()) arrayListOf(
            BtnItems(appContext.getString(R.string.share),R.mipmap.icon_share),
            BtnItems(appContext.getString(if (data.isSave == true) R.string.cancel_saved else R.string.saved),R.mipmap.icon_saved),
            BtnItems(appContext.getString(R.string.report),R.mipmap.icon_report),
            BtnItems(appContext.getString(R.string.hide_post),R.mipmap.icon_hide),
        ) else arrayListOf(
            BtnItems(appContext.getString(R.string.share),R.mipmap.icon_share),
            BtnItems(appContext.getString(R.string.edit),R.mipmap.icon_post_edit),
            BtnItems(appContext.getString(R.string.delete),R.mipmap.icon_del_post,0xfff95862.toInt()),
        )

    fun create() =
        BottomSheetListDialog(context.requireContext(),moreList) { position ->
            when (moreList[position].text) {
                R.string.share.str() -> {
                    SharePostListDialogFragment.crate(context,data).show(context.childFragmentManager,"share")
                }
                R.string.saved.str(),R.string.cancel_saved.str() -> model.saveInvitation(data.invitationId)
                R.string.report.str() -> ReportPostDialog(context.requireContext()) {
                    model.reportInvitation(
                        data.invitationId,
                        it
                    )
                }.show(context.childFragmentManager,"report")
                R.string.edit.str() -> {
                    val postDetailFrg = (context as? PostDetailFrg) ?: return@BottomSheetListDialog
                    postDetailFrg.nav().navigate(
                        R.id.action_postDetailFrg_to_editPostFrg,
                        bundleOf("data" to data,"popId" to postDetailFrg.nav().currentDestination?.id)
                    )
                }
                R.string.delete.str() -> {
                    BottomCancelDialog(
                        context.requireContext(),
                        title = "Delete Post",
                        message = "This operation cannot be undone, Are you sure you want to delete the post?",
                        stroke = 1,
                        confirmText = R.string.delete.str()
                    ) {
                        model.delInvitation(data.invitationId)
                    }.show(context.childFragmentManager,"cancel")
                }
                R.string.hide_post.str() -> {
                    BottomCancelDialog(
                        context.requireContext(),
                        titleColor = R.color.T1,
                        title = "Hide Post?",
                        stroke = 1,
                        confirmText = "Hide"
                    ) {
                        model.hideInvitation(data.invitationId)
                    }.show(context.childFragmentManager,"cancel")
                }
            }
        }.show(context.childFragmentManager,"post")
}